/**
 *	@file	pgskin_sub.c
 *	@brief	ポケギア　スキンアプリ　サブ関数群
 *	@author	Miyuki Iwasawa
 *	@date	07.09.19
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/buflen.h"
#include "gflib/touchpanel.h"
#include "field/field_matrix.h"
#include "field/field_place_name.h"
#include "application/app_tool.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"

#include "../pgear_sys.h"
#include "../pgear_frm.h"
#include "pgskin_sys.h"

#define __BGSKIN_SUB_H_GLOBAL
#include "pgskin_sub.h"



/**
 *	@brief	キャラクタ＆パレットリソース　VRAM転送
 */
void PGSkinSub_2DResInit(PGSKIN_SYS_WORK* wk)
{
	ARCHANDLE* handle;
	int	arcID;

	arcID = ARC_PGSKIN_GRA;

	//アーカイブハンドル取得
	handle = ArchiveDataHandleOpen(arcID,wk->heapID);

	//キャラクタ転送
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgskin_gra_skbg001_ncgr+wk->skin,
		GF_BGL_FRAME3_M,0,0,0);

	//スクリーン転送
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgskin_gra_skbg001_nscr+wk->skin,
		GF_BGL_FRAME3_M,1,0,0);

	//スクリーンリソース取得
	wk->pSBufBg = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_pgskin_gra_skbgs001_nscr+wk->skin,FALSE,
			&wk->pSBg,wk->heapID);

	//ハンドル閉じる
	ArchiveDataHandleClose( handle );

	//スクリーン転送
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME3_M);
}

/**
 *	@brief	キャラクタ＆パレットリソース　VRAM転送
 */
void PGSkinSub_2DResRelease(PGSKIN_SYS_WORK* wk)
{
	sys_FreeMemoryEz(wk->pSBufBg);
}
/**
 *	@brief	パレットアニメワークセット
 */
void PGSkinSub_PalAnmWorkSet(PGSKIN_SYS_WORK* wk,BOOL init_f)
{
	u8	evy;
	int	arcID;
	ARCHANDLE* handle;

	//アーカイブハンドル取得
	arcID = ARC_PGSKIN_GRA;
	handle = ArchiveDataHandleOpen(arcID,wk->heapID);

	//パレットアニメワークに登録
	PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
		NARC_pgskin_gra_skbg001_nclr+wk->skin,wk->heapID,
		FADE_MAIN_BG,FADE_PAL_ONE_SIZE*PGAPP_PALNUM_M,16*PGAPP_PALIDX_M,16*PGAPP_PALIDX_M);
	PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
		NARC_pgskin_gra_skbg001_nclr+wk->skin,wk->heapID,
		FADE_SUB_BG,FADE_PAL_ONE_SIZE*PGAPP_PALNUM_S,16*PGAPP_PALIDX_S,16*PGAPP_PALIDX_S);

	if(init_f){
		//パレットアニメワークに登録
		PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
			NARC_pgskin_gra_oam_sk_nclr,wk->heapID,
			FADE_MAIN_OBJ,FADE_PAL_ONE_SIZE*(PGAPPOAM_PALNUM_M-1),16*PGAPPOAM_PALIDX_M,0);
		PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
			NARC_pgskin_gra_oam_sk_nclr,wk->heapID,
			FADE_SUB_OBJ,FADE_PAL_ONE_SIZE*(PGAPPOAM_PALNUM_S-1),16*PGAPPOAM_PALIDX_S,0);
	}

	//ブラックアウト状態に変更しておく
	PaletteTrans_AutoSet(wk->sys->palAnm,TRUE);
	if(init_f){
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,16*PGAPP_PALIDX_M,16*PGAPP_PALNUM_M,16,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,16*PGAPPOAM_PALIDX_M,16*PGAPPOAM_PALNUM_M,16,0x0000);
	}else{
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,0,16*16,0,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,0,16*16,0,0x0000);
	}
	PaletteFadeTrans( wk->sys->palAnm );
	PaletteTrans_AutoSet(wk->sys->palAnm,FALSE);

	//ハンドル閉じる
	ArchiveDataHandleClose( handle );
}


/**
 *	@brief	スキン選択状態の描画
 */
void PGSkinSub_SelectSkinChange(PGSKIN_SYS_WORK* wk,PG_SKIN_ID id)
{
	wk->skin = id;

	GF_BGL_ScrClear(wk->sys->bgl,GF_BGL_FRAME2_M);

	GF_BGL_ScrWriteExpand(wk->sys->bgl,GF_BGL_FRAME2_M,
		(SM01_PX+(id%3)*SM_OFSX)-1,
		(SM01_PY+(id/3)*SM_OFSY)-1,
		SM_SX+3,SM_SY+2,
		wk->pSBg->rawData,0,0,
		wk->pSBg->screenWidth/8,wk->pSBg->screenHeight/8);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME2_M);
}

/**
 *	@brief	スキンアプリ画面の初期描画
 */
void PGSkinSub_BackDraw(PGSKIN_SYS_WORK* wk)
{
	int i;
	u16	flag = 1;
	//オープンしていないスキンは隠す
	for(i = 0;i < PGSKIN_ID_MAX;i++){
		if(wk->skin_f & flag){
			flag <<= 1;
			continue;
		}
		GF_BGL_ScrWriteExpand(wk->sys->bgl,GF_BGL_FRAME3_M,
			SM01_PX+(i%3)*SM_OFSX,SM01_PY+(i/3)*SM_OFSY,
			SM_SX,SM_SY,
			wk->pSBg->rawData,SM_SX,0,
			wk->pSBg->screenWidth/8,wk->pSBg->screenHeight/8);
		flag <<= 1;
	}
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME3_M);

	//選択状態を反映
	PGSkinSub_SelectSkinChange(wk,wk->skin);
}


/**
 *	@brief	スキンのタッチからキーへの描画切替
 */
void PGSkinSub_ToKeyChange(void* app_wk)
{
	PGSKIN_SYS_WORK* wk = (PGSKIN_SYS_WORK*)app_wk;

	//選択カーソル表示
	CursorMvMan_SetCurrent(wk->sys->curMan,PGKM_APP_PARAM1,0xFF);
	CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_SYS_PARAM,FALSE);
	CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_APP_PARAM1,TRUE);
}

/**
 *	@brief	スキンのシステムキーからタッチでアプリに描画切替
 */
void PGSkinSub_ToTouchChange(PGSKIN_SYS_WORK* wk)
{
	//選択カーソル表示
	wk->sys->control = PGCONT_APP;
	CursorMvMan_SetCurrent(wk->sys->curMan,PGKM_APP_PARAM1,0xFF);
	CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_SYS_PARAM,FALSE);
	CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_APP_PARAM1,TRUE);
}

///セレクトボックス　ヘッダデータ構造体
static const SELBOX_HEAD_PRM data_sbox_prm = {
	TRUE,SBOX_OFSTYPE_CENTER,	///<ループフラグ、左寄せorセンタリング

	0,	///<項目表示オフセットX座標(dot)

	GF_BGL_FRAME1_M,		///<フレームNo
	SBOX_WPAL,				///<BGパレットID
	1,1,
	SBOX_FCGX,SBOX_WCGX,	///<ウィンドウcgx,フレームcgx
	SBOX_FCGX_SIZ,		///<占有キャラクタ領域サイズ(char)
};

struct SKIN_SBOX_POS{
	u8	x,y;
};

static const struct SKIN_SBOX_POS data_sbox_pos[] = {
	{10,SM01_PY-1},{20,SM02_PY-1},{15,SM03_PY-1},
	{10,SM04_PY-1},{20,SM05_PY-1},{15,SM06_PY-1},
};

/**
 *	@brief	セレクトボックスの表示
 */
void PGSkinSub_SelBoxSet(PGSKIN_SYS_WORK* wk,u8 pos)
{
	int i;
	u8	key_mode;
	SELBOX_HEADER head;

	//今選んだデザインNoを選んでおく
	wk->tmpSkin = pos;

	MI_CpuClear8(&head,sizeof(SELBOX_HEADER));

	head.prm = data_sbox_prm;
	head.list = (const BMPLIST_DATA*)wk->plist;
	head.bgl = wk->sys->bgl;

	head.count = SBOX_COUNT;

	wk->list_wk = SelectBoxSet(wk->sbox,&(head),wk->sys->key_mode,
			data_sbox_pos[pos].x,data_sbox_pos[pos].y,0,0);
	PGSkinSub_WindowMaskSet(wk,pos,TRUE);

	CursorMvMan_SetAnmFlagIdx(wk->sys->curMan,CURMV_CURRENT,FALSE);
}

/**
 *	@brief	ウィンドウマスク設定
 */
void PGSkinSub_WindowMaskSet(PGSKIN_SYS_WORK* wk,u8 pos,BOOL flag)
{
	int	px,py,w;

	if(flag){	//on
		G2_SetWnd0InsidePlane(	GX_WND_PLANEMASK_BG0|GX_WND_PLANEMASK_BG1|
								GX_WND_PLANEMASK_BG2|GX_WND_PLANEMASK_BG3|
								GX_WND_PLANEMASK_OBJ, FALSE);
		G2_SetWnd1InsidePlane(	GX_WND_PLANEMASK_BG0|GX_WND_PLANEMASK_BG1|
								GX_WND_PLANEMASK_BG2|GX_WND_PLANEMASK_BG3|
								GX_WND_PLANEMASK_OBJ, FALSE);
		G2_SetWndOutsidePlane(	GX_WND_PLANEMASK_BG0|GX_WND_PLANEMASK_BG1|
								GX_WND_PLANEMASK_BG2|GX_WND_PLANEMASK_BG3|
								GX_WND_PLANEMASK_OBJ, TRUE);

		px = (data_sbox_pos[pos].x*8);
		py = (data_sbox_pos[pos].y*8);
		w = (wk->list_wk->width+2)*8;

		//左上XY,右上XY
		G2_SetWnd0Position( px, py, px+w, py+(56));
		G2_SetWnd1Position( SM01_DPX+(pos%3)*SM_OFSDX,SM01_DPY+(pos/3)*SM_OFSDY,
							SM01_DPX+(pos%3)*SM_OFSDX+SM_DSX,SM01_DPY+(pos/3)*SM_OFSDY+SM_DSY);

		GX_SetVisibleWnd(GX_WNDMASK_W0|GX_WNDMASK_W1);
		G2_SetBlendBrightness(	GX_BLEND_PLANEMASK_BG0|GX_BLEND_PLANEMASK_BG1|
								GX_BLEND_PLANEMASK_BG2|GX_BLEND_PLANEMASK_BG3|
								GX_BLEND_PLANEMASK_OBJ,-8);
	}else{	//off
		G2_SetWnd0InsidePlane(	GX_WND_PLANEMASK_NONE, FALSE);
		G2_SetWnd1InsidePlane(	GX_WND_PLANEMASK_NONE, FALSE);
		G2_SetWndOutsidePlane(	GX_WND_PLANEMASK_NONE, FALSE);
		GX_SetVisibleWnd(GX_WNDMASK_NONE);
		G2_SetBlendBrightness(	GX_BLEND_PLANEMASK_BG0|GX_BLEND_PLANEMASK_BG1|
								GX_BLEND_PLANEMASK_BG2|GX_BLEND_PLANEMASK_BG3|
								GX_BLEND_PLANEMASK_OBJ,0);
		G2_BlendNone();
	}
}